package com.itplh.java8.stream;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author: tanpeng
 * @date: 2019-06-17 11:54
 * @desc:
 */
public class ApiTest {

    public static void main(String[] args) {
        System.out.println("****************list == null******************");
        List<Integer> list = null;
        testStreamApi(list);
        System.out.println("****************list != null******************");
        List<Integer> integers = Arrays.asList(new Integer[]{12, 1, 2, 3, 4, 5, 6, 7});
        testStreamApi(integers);

        List<Integer> integers1 = Arrays.asList(new Integer[]{});
        System.out.println(integers1.stream().findFirst().orElseGet(() -> new Integer(-1)));
        System.out.println(Integer.MAX_VALUE);
    }

    private static void testStreamApi(List<Integer> integerList) {
        if (!Optional.ofNullable(integerList).isPresent()) {
            System.out.println("integerList is null");
            return;
        }
        integerList.stream().forEach(System.out::println);
        System.out.println("--------------forEach---------------------");

        integerList.stream().map(i -> i * 2).forEach(System.out::println);
        System.out.println("--------------map------------------");
        System.out.println("--------------flatMap --> 不理解------------------");
        integerList.stream().filter(i -> i > 4).forEach(System.out::println);
        System.out.println("--------------filter i>4------------------");
        integerList.stream().limit(1).forEach(System.out::println);
        System.out.println("--------------limit 1---------------------");
        integerList.stream().findFirst().ifPresent(System.out::println);
        System.out.println("--------------findFirst-------------------");
    }
}
